--Need to make an empy table to hold the state when 'require' is called at the beginning
mainMenu = {}

menuMusic = love.audio.newSource('mainMenu/Sonar1.ogg')
menuMusic:setLooping(true)
menuMusic:setVolume(1)

--prepare constants
local twoPI = math.pi * 2
local halfPI = math.pi / 2
local buttonScale = 1920-(1080*.85)
local buttonColorOff = { r=0, g=255, b=0, a=255 }
local buttonColorOn = { r=255, g=0, b=0, a=255 }

--Called the first time the state is loaded
function mainMenu:init()
  love.audio.play(menuMusic)
  --Load the images to be used
  menuPlay = love.graphics.newImage('mainMenu/menuPlay.png')
  menuHelp = love.graphics.newImage('mainMenu/menuHelp.png')
  menuQuit = love.graphics.newImage('mainMenu/menuQuit.png')
  --Sophisticated Sparrows
  ssg = love.graphics.newImage('mainMenu/SSGsmall.png')
  --Fullscreen toggle images
  fullOff = love.graphics.newImage('helpMenu/fullscreenOff.png')
  fullOn = love.graphics.newImage('helpMenu/fullscreenOn.png')
  --Audio toggle
  audioOff = love.graphics.newImage('helpMenu/audioOff.png')
  audioOn = love.graphics.newImage('helpMenu/audioOn.png')
  isAudioOn = true
  
  --This table holds all of the graphics for the radar.
  mmRadar = {}
  --The fade that is seen on the radar
  mmRadar.fade = {}
  mmRadar.fade.i = love.graphics.newImage('mainMenu/mmRadarFade.png')
  --Same X and Y cooridinates are used by all of the radar images, but since the fade rotates,
  --it also needs a rotational value
  mmRadar.fade.x = mmRadar.fade.i:getWidth()/2
  mmRadar.fade.y = 1080/2 
  mmRadar.fade.r = 0
  --Game title
  mmRadar.title = {}
  mmRadar.title.i = love.graphics.newImage('mainMenu/mmRadarText.png')
  --Circle that surrounds the radar
  mmRadar.circle = {}
  mmRadar.circle.i = love.graphics.newImage('mainMenu/mmRadarCircle.png')
  --The rotating line
  mmRadar.line = {}
  mmRadar.line.i = love.graphics.newImage('mainMenu/mmRadarLine.png')
  
  --This holds what button the mouse is over. Buttons start at 0 and increase from there.
  --So '-1' means that there is no selection.
  mouseSelection = -1
end
--
--This is called everytime the state is entered.
function mainMenu:enter()
  love.mouse.setVisible(true)
  mouseSelection = -1
end
--
--Called every frame, 'dt' is the time since the last frame
function mainMenu:update(dt)
  --Get the mouse position
  mx,my = love.mouse.getPosition()
  
  --Rotate the radar. When it completes a rotation, it resets.
  --Possibly an unnecessary concern, but just letting it increase could lead to an issue after too long?
  mmRadar.fade.r = mmRadar.fade.r + (halfPI)*dt
  if mmRadar.fade.r >= (twoPI) then
    mmRadar.fade.r = 0
  end
  
  --Update the button the mouse is over.
  
  --Audio Button
  if mx >= (1688*screen.scale) and mx <= (1900*screen.scale) and my >= (1024*screen.scale) and my <= (1060*screen.scale) then
    mouseSelection = 4
  --Every button within this section is on the menu on the right
  elseif mx >= (1370 * screen.scale) and mx <= (1712 * screen.scale) then
    --Play button
    if my >= (150*screen.scale) and my <= (249*screen.scale) then
      mouseSelection = 0
      --Help button
    elseif my >= (491*screen.scale) and my <= (590*screen.scale) then
      mouseSelection = 1
      --Quit button
    elseif my >= (831*screen.scale) and my <= (930*screen.scale) then
      mouseSelection = 2
    else
      --Otherwise no selection
      mouseSelection = -1
    end
  --This is for the fullscreen button
  elseif mx >= (20*screen.scale) and mx <= (395*screen.scale) and my >= (1024*screen.scale) and my <= (1060*screen.scale) then
    mouseSelection = 3
  else
    mouseSelection = -1
  end
end
--
--Called every frame to draw whatever is put here
function mainMenu:draw()
  --Scale the graphics to the appropriate size
  love.graphics.scale(screen.scale,screen.scale)
  
  --Radar: Drawing everything related to the radar
  --Most images in the game are white, and then colored at runtime
  love.graphics.setColor(0,255,255,255)
  love.graphics.draw(mmRadar.title.i)
  love.graphics.draw(ssg,1080/2-ssg:getWidth()/2,(1080-108-ssg:getHeight())*.95)
  love.graphics.setColor(255,255,255,255)
  love.graphics.draw(mmRadar.fade.i, mmRadar.fade.x,mmRadar.fade.y,mmRadar.fade.r,1,1,mmRadar.fade.i:getWidth()/2,mmRadar.fade.i:   getHeight()/2)
  love.graphics.draw(mmRadar.line.i, mmRadar.fade.x,mmRadar.fade.y,mmRadar.fade.r,1,1,mmRadar.fade.i:getWidth()/2,mmRadar.fade.i:getHeight()/2)
  love.graphics.draw(mmRadar.circle.i)
  
  --Menu Selection: Highlight the current button in red, and everything else in green.
  if mouseSelection == 0 then
    drawButton(menuPlay, buttonColorOn)
    drawButton(menuHelp, buttonColorOff)
    drawButton(menuQuit, buttonColorOff)
    --Which image to draw for the fullscreen button is dependant on whether or not fullscreen is on
    love.graphics.setColor(0,255,0,255)
    if isFullscreen then 
      love.graphics.draw(fullOn) 
    else 
      love.graphics.draw(fullOff) 
    end
    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end
    
  elseif mouseSelection == 1 then
    
    drawButton(menuPlay, buttonColorOff)
    drawButton(menuHelp, buttonColorOn)
    drawButton(menuQuit, buttonColorOff)

    love.graphics.setColor(0,255,0,255)
    if isFullscreen then 
      love.graphics.draw(fullOn) 
    else 
      love.graphics.draw(fullOff) 
    end
    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end
    
  elseif mouseSelection == 2 then
    drawButton(menuPlay, buttonColorOff)
    drawButton(menuHelp, buttonColorOff)
    drawButton(menuQuit, buttonColorOn)
    
    love.graphics.setColor(0,255,0,255)
    if isFullscreen then 
      love.graphics.draw(fullOn) 
    else 
      love.graphics.draw(fullOff) 
    end
    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end

  elseif mouseSelection == 3 then
    drawButton(menuPlay, buttonColorOff)
    drawButton(menuHelp, buttonColorOff)
    drawButton(menuQuit, buttonColorOff)
    
    love.graphics.setColor(255,0,0,255)
    if isFullscreen then
      love.graphics.draw(fullOn)
    else
      love.graphics.draw(fullOff)
    end
    love.graphics.setColor(0,255,0,255)    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end
  elseif mouseSelection == 4 then
    drawButton(menuPlay, buttonColorOff)
    drawButton(menuHelp, buttonColorOff)
    drawButton(menuQuit, buttonColorOff)
    
    love.graphics.setColor(0,255,0,255)
    if isFullscreen then
      love.graphics.draw(fullOn)
    else
      love.graphics.draw(fullOff)
    end
    love.graphics.setColor(255,0,0,255)    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end
    
  else
    
    drawButton(menuPlay, buttonColorOff)
    drawButton(menuHelp, buttonColorOff)
    drawButton(menuQuit, buttonColorOff)
    
    if isFullscreen then 
      love.graphics.draw(fullOn) 
    else 
      love.graphics.draw(fullOff) 
    end
    
    if isAudioOn then
      love.graphics.draw(audioOn)
    else
      love.graphics.draw(audioOff)
    end
    
  end
end

--helper to draw a button in a specific color.
function drawButton(button, color)
    love.graphics.setColor(color.r, color.g, color.b, color.a)
    love.graphics.draw(button, buttonScale, 0)
end

--
--Is called whenever a key is pressed. 'k' is that key.
function mainMenu:keyreleased(k)
end
function mainMenu:keypressed(k)
end
--
--In order to ensure that a button is only triggered when the player clicks and releases the button
--without moving their mouse off of it at any point, we store the intial button selection when the mouse
--is pressed down. 'b' is the button pressed on the mouse, and 'x' and 'y' are the coordinates of the mouse
--at that time
function mainMenu:mousepressed(x,y,b)
  if b == 'l' then
    mousePrevSelection = mouseSelection
  end
end

--If the player releases the mouse over the same button they clicked on, then trigger the button.
function mainMenu:mousereleased(x,y,b)
  if b == "l" then
    if mouseSelection == 0 and mousePrevSelection == 0 then
      --Run the game. There is a short delay here while the game is loaded, mainly due to a
      --procedurally generated map being created. As of right now, the game loads quickly enough
      --to not necessarily justify a load screen, but we can add that in if we want.
      love.audio.stop(menuMusic)
      Gamestate.switch(game)
    elseif mouseSelection == 1 and mousePrevSelection == 1 then
      --Switch to the help menu
      Gamestate.switch(helpMenu)
    elseif mouseSelection == 2 and mousePrevSelection == 2 then
      --Exit the program
      love.event.quit()
    elseif mouseSelection == 3 and mousePrevSelection == 3 then
      --This toggles fullscreen on and off and scales to the native resolution of the monitor
      --while keeping a correct aspect ratio.
      isFullscreen = not isFullscreen
      toggleFullscreen()
    elseif mouseSelection == 4 and mousePrevSelection == 4 then
      isAudioOn = not isAudioOn
      if isAudioOn then
        love.audio.setVolume(1)
      else
        love.audio.setVolume(0)
      end
    end
  end
end
